home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / HPPARMS.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  9KB  |  322 lines

  1. /*
  2.    Module:  hpparms.c
  3.    Date:    3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file ADI7221.DOC.
  15.  
  16.    Purpose: This module provides the functions required to convert ADI
  17.             code arguments to HP parameter codes.
  18.  
  19.    Functions provided:
  20.  
  21.         uinttosbn    (unsigned int to Single Byte Number)
  22.         uiptombp     (unsigned int pair to Multiple Byte Pair)
  23. */
  24.  
  25. #ifndef TRUE
  26. #define TRUE 1
  27. #endif
  28. #ifndef FALSE
  29. #define FALSE 0
  30. #endif
  31.  
  32. /*  Prototypes for private functions */
  33. int calcbytes (unsigned larger);
  34. void twobyte (unsigned num1, unsigned num2, char *mbpout);
  35. void threebyte (unsigned num1, unsigned num2, char *mbpout);
  36. void fourbyte (unsigned num1, unsigned num2, char *mbpout);
  37. void fivebyte (unsigned num1, unsigned num2, char *mbpout);
  38.  
  39. /*
  40.    Function: uinttosbn
  41.    Purpose:  Convert an unsigned int to an HP Single Byte Number.
  42.  
  43.    Pre: numin is the number to convert.
  44.         0 <= numin <= 63
  45.         sbnout is a pointer to storage for the converted Single Byte Number.
  46.  
  47.    Post: An attempt is made to convert numin to SBN format and copy the
  48.          retult to sbnout.
  49.          If an error occurs (ie numin is out of range), FALSE is returned.
  50.          Otherwise, TRUE is returned.
  51. */
  52.  
  53. int uinttosbn (numin, sbnout)
  54.    unsigned numin;
  55.    char *sbnout;
  56. {
  57.    if (numin > 63)
  58.       return (FALSE);
  59.    *sbnout = (char) (numin > 31) ? numin : numin+64;
  60.    return (TRUE);
  61. }
  62.  
  63.  
  64.  
  65. /*
  66.    Function: uiptombp
  67.    Purpose: Convert a pair of unsigned ints to an HP Multiple Byte Pair.
  68.  
  69.    Pre: num1 and num2 comprise the pair of numbers to be converted to an
  70.         HP MBP.
  71.         0 <= num1,num2 <= 16383
  72.         mbpout is a pointer to storage for the converted code.
  73.         mbpout points to enough storage to hold the maximum size of a mbp
  74.         formatted code (MAXMBPLEN).
  75.         mbpsize is a pointer to storage for the number of bytes actually
  76.         placed in mbpout.
  77.  
  78.    Post: an attempt is made to convert num1 and num2 to MBP format and copy
  79.          the result to mbpout.
  80.          The number of bytes in mbpout is returned in mbpsize.
  81.          If an error occurs (num1 or num2 are out of range), FALSE is
  82.          returned.
  83.          Otherwise, TRUE is returned.
  84.  
  85. */
  86.  
  87. int uiptombp (num1, num2, mbpout, mbpsize)
  88.    unsigned num1, num2;
  89.    char *mbpout;
  90.    int *mbpsize;
  91. {
  92.    unsigned larger;
  93.  
  94.    if ((num1 > 16383) || (num2 > 16383))
  95.       return (FALSE);
  96.  
  97.    larger = (num1 > num2) ? num1 : num2;
  98.    *mbpsize = calcbytes (larger);
  99.  
  100.    switch (*mbpsize) {
  101.       case 1:
  102.          /* the following formula is explained on p208 of the HP manual */
  103.          *mbpout=num2 + 96 + 4 * num1;
  104.          break;
  105.       case 2:
  106.          twobyte(num1,num2,mbpout);
  107.          break;
  108.       case 3:
  109.          threebyte(num1,num2,mbpout);
  110.          break;
  111.       case 4:
  112.          fourbyte(num1,num2,mbpout);
  113.          break;
  114.       case 5:
  115.          fivebyte(num1,num2,mbpout);
  116.          break;
  117.    }
  118.    return (TRUE);
  119. }
  120.  
  121. /*
  122.    Function: calcbytes
  123.    Purpose:  This private procedure calculates the number of bytes needed to 
  124.              store a Multiple Byte Pair.
  125.  
  126.              This procedure is based on the flow chart on page 207 of the
  127.              "Hewlett-Packard 7221A Graphics Plotter Operating and
  128.              Programming Manual."
  129.  
  130.    Pre: larger is the larger of the unsigned int pair.
  131.         It has already been determined that 0 <= larger <= 16383.
  132.  
  133.    Post: The number of bytes needed to store the MBP is returned.
  134. */
  135.  
  136. int calcbytes (larger)
  137.    unsigned larger;
  138. {
  139.    if (larger < 256)
  140.       if (larger > 31)
  141.          return (3);
  142.       else if (larger > 3)
  143.          return (2);
  144.       else
  145.          return (1);
  146.    else if (larger < 2048)
  147.       return (4);
  148.    else if (larger < 16384)
  149.       return (5);
  150.  
  151.    /* we should never reach this point */
  152.    return (0);
  153. }
  154.  
  155.  
  156. /*
  157.    Function: twobyte
  158.    Purpose:  This private procedure calculates a Multiple Byte Pair when it 
  159.              is known that unsigned int pair is valid and will require two 
  160.              bytes for an MBP representation.
  161.  
  162.              The procedure is based on the flowchart at the bottom of page 
  163.              208 in the "Hewlett-Packard 7221A Graphics Plotter Operating and
  164.              Programming Manual."
  165.  
  166.    Pre: It has been determined that num1 and num2 are in the range that will
  167.         produce a two byte MBP.
  168.         mbpout is a pointer to storage for the two byte MBP.
  169.         mbpout points to at least two bytes of storage.
  170.  
  171.    Post: num1 and num2 are converted to a MBP that is stored in mbpout.
  172. */
  173.  
  174. void twobyte (num1, num2, mbpout)
  175.    unsigned num1, num2;
  176.    char *mbpout;
  177. {
  178.    unsigned x1, x2;
  179.  
  180.    x1 = num1 / 2;
  181.    x2 = num1 -2 * x1;
  182.    *mbpout++ = (char) (x1 + 96);
  183.    *mbpout = (char) (num2 + 32 * x2);
  184.    if (*mbpout <= (char) 31)
  185.       *mbpout += (char) 64;
  186. }
  187.  
  188.  
  189. /*
  190.    Function: threebyte
  191.    Purpose:  This private procedure calculates a Multiple Byte Pair when it 
  192.              is known that unsigned int pair is valid and will require three
  193.              bytes for an MBP representation.
  194.  
  195.              The following procedure is based on the flowchart on p209 of
  196.              the "Hewlett-Packard 7221A Graphics Plotter Operating and
  197.              Programming Manual."
  198.  
  199.    Pre: It has been determined that num1 and num2 are in the range that will
  200.         produce a three byte MBP.
  201.         mbpout is a pointer to storage for the three byte MBP.
  202.         mbpout points to at leat three bytes of storage.
  203.  
  204.    Post: num1 and num2 are converted to a MBP that is stored in mbpout.
  205. */
  206.  
  207. void threebyte (num1, num2, mbpout)
  208.    unsigned num1, num2;
  209.    char *mbpout;
  210. {
  211.    unsigned x1, x2, y2, y3;
  212.  
  213.    x1 = num1 / 16;
  214.    x2 = num1 - 16 * x1;
  215.    y2 = num2 / 64;
  216.    y3 = num2 - 64 * y2;
  217.    *mbpout++ = (char) (x1 + 96);
  218.    *mbpout = (char) (y2 + 4 * x2);
  219.    if (*mbpout <= (char) 31)
  220.       *mbpout += 64;
  221.    mbpout++;
  222.    *mbpout = (char) (y3);
  223.    if (*mbpout <= (char) 31)
  224.       *mbpout += 64;
  225. }
  226.  
  227.  
  228. /*
  229.    Function: fourbyte
  230.    Purpose:  This private procedure calculates a Multiple Byte Pair when it 
  231.              is known that unsigned int pair is valid and will require four
  232.              bytes for an MBP representation.
  233.  
  234.              This procedure is based on the flow chart on p210 of the
  235.              "Hewlett-Packard 7221A Graphics Plotter Operating and
  236.              Programming Manual."
  237.  
  238.    Pre: It has been determined that num1 and num2 are in the range that will
  239.         produce a four byte MBP.
  240.         mbpout is a pointer to storage for the four byte MBP.
  241.         mbpout points to at leat four bytes of storage.
  242.  
  243.    Post: num1 and num2 are converted to a MBP that is stored in mbpout.
  244. */
  245.  
  246. void fourbyte (num1, num2, mbpout)
  247.    unsigned num1, num2;
  248.    char *mbpout;
  249. {
  250.    unsigned x1, xr, x2, x3, y3, y4;
  251.  
  252.    x1 = num1 / 128;
  253.    xr = num1 - 128 * x1;
  254.    x2 = xr / 2;
  255.    x3 = xr - 2 * x2;
  256.    y3 = num2 / 64;
  257.    y4 = num2 - 64 * y3;
  258.    *mbpout++ = (char) (x1 + 96);
  259.    *mbpout = (char) (x2);
  260.    if (*mbpout <= (char) 31)
  261.       *mbpout += 64;
  262.    mbpout++;
  263.    *mbpout = (char) (y3 + 32 * x3);
  264.    if (*mbpout <= (char) 31)
  265.       *mbpout += 64;
  266.    mbpout++;
  267.    *mbpout = (char) (y4);
  268.    if (*mbpout <= (char) 31)
  269.       *mbpout += 64;
  270. }
  271.  
  272.  
  273. /*
  274.    Function: fivebyte
  275.    Purpose:  This private procedure calculates a Multiple Byte Pair when it 
  276.              is known that unsigned int pair is valid and will require five
  277.              bytes for an MBP representation.
  278.  
  279.              This procedure is based on the flow chart on p211 of the
  280.              "Hewlett-Packard 7221A Graphics Plotter Operating and
  281.              Programming Manual."
  282.  
  283.    Pre: It has been determined that num1 and num2 are in the range that will
  284.         produce a five byte MBP.
  285.         mbpout is a pointer to storage for the five byte MBP.
  286.         mbpout points to at leat five bytes of storage.
  287.  
  288.    Post: num1 and num2 are converted to a MBP that is stored in mbpout.
  289. */
  290.  
  291. void fivebyte (num1, num2, mbpout)
  292.    unsigned num1, num2;
  293.    char *mbpout;
  294. {
  295.    unsigned x1, xr, x2, x3, y3, yr, y4, y5;
  296.  
  297.    x1 = num1 / 1024;
  298.    xr = num1 - 1024 * x1;
  299.    x2 = xr / 16;
  300.    x3 = xr - 16 * x2;
  301.    y3 = num2 / 4096;
  302.    yr = num2 - 4096 * y3;
  303.    y4 = yr / 64;
  304.    y5 = yr - 64 * y4;
  305.    *mbpout++ = (char) (x1 + 96);
  306.    *mbpout = (char) (x2);
  307.    if (*mbpout <= (char) 31)
  308.       *mbpout += 64;
  309.    mbpout++;
  310.    *mbpout = (char) (y3 + 4 * x3);
  311.    if (*mbpout <= (char) 31)
  312.       *mbpout += 64;
  313.    mbpout++;
  314.    *mbpout = (char) (y4);
  315.    if (*mbpout <= (char) 31)
  316.       *mbpout += 64;
  317.    mbpout++;
  318.    *mbpout = (char) (y5);
  319.    if (*mbpout <= (char) 31)
  320.       *mbpout += 64;
  321. }
  322.